package com.xhu.collegestudentrentalsystem.module.user.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xhu.collegestudentrentalsystem.annocation.LoginRequired;
import com.xhu.collegestudentrentalsystem.module.user.entity.Feedback;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.module.user.entity.HouseScore;
import com.xhu.collegestudentrentalsystem.module.user.entity.User;
import com.xhu.collegestudentrentalsystem.module.user.service.impl.FeedbackServiceImpl;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import com.xhu.collegestudentrentalsystem.util.constant.StatusMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/29 12:50
 */
@Controller
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackServiceImpl feedbackService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 转到反馈页面
     *      需要查询用户是否已提交过评分
     *      如果提交过，则不显示评分部分
     */
    @LoginRequired
    @RequestMapping("feedbackPage")
    public String feedbackPage(Long orderId, Model model){
        HouseScore score = feedbackService.findHouseScore(orderId, hostHolder.getUser().getId());
        if(score != null){
            model.addAttribute("noScore", false);
        }else {
            model.addAttribute("noScore", true);
        }
        model.addAttribute("orderId", orderId);
        return "site/feedback";
    }

    /**
     * 用户提交反馈信息
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public ResultData submitFeedback(Feedback feedback){
        if (hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        // 用户提交反馈信息，插入数据库表中，等待房东处理
        feedback.setUserId(hostHolder.getUser().getId());
        feedback.setCreateTime(new Date());
        feedback.setStatus(StatusMsg.feedbackStatus.WAIT_DEAL);
        Integer result = feedbackService.submitFeedbackInfo(feedback);
        if(result > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.feedback.SUBMIT_SUCCESS);
        }else {
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.feedback.SUBMIT_FAIL);
        }
    }

    /**
     * 我的反馈信息
     */
    @LoginRequired
    @RequestMapping("/feedbackList")
    public String feedbackList(Page page, Model model){
        page.setLimit(5);
        List<Feedback> feedbackList = feedbackService.findCustomerFeedbackList(hostHolder.getUser().getId(), page);
        model.addAttribute(KeyMsg.FEEDBACK_LIST, feedbackList);
        page.setPath("/feedback/feedbackList");
        return "site/feedback-list";
    }

    /**
     * 我的评分反馈
     */
    @RequestMapping("/submitScoreInfo")
    @ResponseBody
    public ResultData submitScoreInfo(HouseScore houseScore){
        User user = hostHolder.getUser();
        if(user == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        houseScore.setUserId(user.getId());
        Integer result = feedbackService.submitScoreInfo(houseScore);
        if(result > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.score.SUBMIT_SUCCESS);
        }
        return new ResultData(ResultData.OPERATION_FAIL, PointMsg.score.SUBMIT_FAIL);
    }

    /**
     * 房东回复反馈信息
     *      对反馈表中部分内容(状态、回复)进行修改
     */
    @LoginRequired
    @RequestMapping("/reply")
    public String reply(Long feedbackId, String reply, Integer status){
        feedbackService.updateReplyAndStatus(feedbackId, reply, status);
        return "redirect:/feedback/feedbackList";
    }

}
